<template>
  <mm_warp>
    <mm_container class="container diy_view">
      <mm_row>
        <mm_col>
          <mm_view class="">
            <uni-forms :modelValue="form"  v-if="is_view()">
              <uni-forms-item v-if="$check_field('get','title') || $check_field('add','title') || $check_field('set','title')" label="标题" name="title">
                <uni-easyinput type="text" v-model="form['title']" v-if="user_group === '管理员' || (form['adoption_agreement_id'] && $check_field('set','title')) || (!form['adoption_agreement_id'] && $check_field('add','title'))" :disabled="disabledObj['title_isDisabled']" />
                <!-- 仅查看 -->
                <text v-else-if="$check_field('get','title')">
                  {{ form['title'] }}
                </text>
              </uni-forms-item>

            </uni-forms>
            <view class="form_button">
              <button size="mini" type="primary" @click="submit()">提交</button>
              <button size="mini" @click="cancel()">取消</button>
            </view>
          </mm_view>
        </mm_col>
      </mm_row>
    </mm_container>
  </mm_warp>
</template>

<script>
	import mixin from "@/mixins/page.js";

	export default {
		mixins: [mixin],
		data() {
			return {
              field: "adoption_agreement_id",
              url_add: "~/api/adoption_agreement/add?",
              url_set: "~/api/adoption_agreement/set?",
              url_get_obj: "~/api/adoption_agreement/get_obj?",
              url_upload: "~/api/adoption_agreement/upload?",

              query: {
                "adoption_agreement_id": 0,
              },

              form: {
                "title":'', // 标题
                "text":'', // 正文
                "adoption_agreement_id": 0, // ID
              },
              disabledObj:{
                "title_isDisabled": false,
                "text_isDisabled": false,
              },
			}
		},
		methods: {
          changeLog(v,value){
            this.form[value] = v
          },
          /**
           * 上传文件
           * @param {Object} param文件参数
           */
          change_file(key_name){
            var _self = this;
            // 选择图像方法
            uni.chooseFile({
              count: 1,
              sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
              sourceType: ['album'], //从相册选择
              success: function(res) {
                const tempFilePaths = res.tempFilePaths;
                const uploadTask = uni.uploadFile({
                  url: _self.$fullUrl('/api/adoption_agreement/upload?'),
                  filePath: tempFilePaths[0],
                  name: 'file',
                  formData: {
                    'i_want_to_customize': 'test'
                  },
                  header: {
                    'x-auth-token': _self.$store.state.user.token
                  },
                  success: function(uploadFileRes) {
                    var filename = JSON.parse(uploadFileRes.data).result.url
                    _self.form[key_name] = filename
                  }
                });

                uploadTask.onProgressUpdate(function(res) {
                  _self.percent = res.progress;
                  console.log('上传进度' + res.progress);
                  console.log('已经上传的数据长度' + res.totalBytesSent);
                  console.log('预期需要上传的数据总长度' + res.totalBytesExpectedToSend);
                });
              },
              error: function(e) {
                console.log(e);
              }
            });
          },
          /**
           * 上传图片
           * @param {Object} param文件参数
           */
          change_img(key_name){
            var _self = this;
            // 选择图像方法
            uni.chooseImage({
              count: 1,
              sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
              sourceType: ['album'], //从相册选择
              success: function(res) {
                const tempFilePaths = res.tempFilePaths;
                const uploadTask = uni.uploadFile({
                  url: _self.$fullUrl('/api/adoption_agreement/upload?'),
                  filePath: tempFilePaths[0],
                  name: 'file',
                  formData: {
                    'i_want_to_customize': 'test'
                  },
                  header: {
                    'x-auth-token': _self.$store.state.user.token
                  },
                  success: function(uploadFileRes) {
                    var filename = JSON.parse(uploadFileRes.data).result.url
                    _self.form[key_name] = filename
                  }
                });

                uploadTask.onProgressUpdate(function(res) {
                  _self.percent = res.progress;
                  console.log('上传进度' + res.progress);
                  console.log('已经上传的数据长度' + res.totalBytesSent);
                  console.log('预期需要上传的数据总长度' + res.totalBytesExpectedToSend);
                });
              },
              error: function(e) {
                console.log(e);
              }
            });
          },
          /**
           * 获取对象后获取缓存表单
           * @param {Object} json
           * @param {Object} func
           */
          get_obj_before(param){
            var form = uni.db.get("form");
            if (form) {
              this.obj = uni.push(this.obj ,form);
              this.form = uni.push(this.form ,form);
            }
            var arr = []
            for (let key in form) {
              arr.push(key)
            }
            for (var i=0;i<arr.length;i++){
              this.disabledObj[arr[i] + '_isDisabled'] = true
            }
            uni.db.del("form");
            return param;
          },

          /**
           * 获取对象之后
           * @param {Object} json
           * @param {Object} func
           */
          get_obj_after(json, func){
          },

          is_view(){
            var bl = this.user_group == "管理员";

            if(!bl){
              bl = this.$check_action('/adoption_agreement/table','add');
              console.log(bl ? "你有表格添加权限视作有添加权限" : "你没有表格添加权限");
            }
            if(!bl){
              bl = this.$check_action('/adoption_agreement/table','set');
              console.log(bl ? "你有表格添加权限视作有修改权限" : "你没有表格修改权限");
            }
            if(!bl){
              bl = this.$check_action('/adoption_agreement/view','add');
              console.log(bl ? "你有视图添加权限视作有添加权限" : "你没有视图添加权限");
            }
            if(!bl){
              bl = this.$check_action('/adoption_agreement/view','set');
              console.log(bl ? "你有视图修改权限视作有修改权限" : "你没有视图修改权限");
            }
            if(!bl){
              bl = this.$check_action('/adoption_agreement/view','get');
              console.log(bl ? "你有视图查询权限视作有查询权限" : "你没有视图查询权限");
            }

            console.log(bl ? "具有当前页面的查看权，请注意这不代表你有字段的查看权" : "无权查看当前页，请注意即便有字段查询权限没有页面查询权限也不行");

            return bl;
          },

		},
		created() {
		}
	}
</script>

<style scoped>
	.avatar-uploader .el-upload {
		border: 1px dashed #d9d9d9;
		border-radius: 6px;
		cursor: pointer;
		position: relative;
		overflow: hidden;
	}

	.avatar-uploader .el-upload:hover {
		border-color: #409EFF;
	}

    .form_button{
      padding-bottom: 15px;
      display: flex;
    }
    .form_button button{
      width: 40%;
    }
    .query_select{
      border-color: rgb(229, 229, 229);
      background-color: rgb(255, 255, 255);
      border-radius: 4px;
      box-sizing: border-box;
      flex: 1;
      width: 100%;
      line-height: 2;
      font-size: 14px;
      height: 2.4em;
      min-height: 2.4em;
      display: block;
      outline:none;
    }

    .query_option{
      width: 100%;
    }

    .btn_add_img{
      color: #D3D3D3;
      text-align: center;
      border: 1px solid #eee;
      height: 5rem;
      width: 5rem;
      position: relative;
    }
    .btn_add_img text{
      font-size: 35px;
      position: absolute;
      left: 50%;
      top: 50%;
      transform: translate(-50%,-50%);
    }
</style>
